package edu.shzu.manage.service.impl;

import edu.shzu.manage.dao.RoleDao;
import edu.shzu.manage.entity.Role;
import edu.shzu.manage.service.RoleService;
import edu.shzu.manage.vo.RoleVo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Override
    public Role getRoleById(Integer roleId) {
        return roleDao.getRoleById(roleId);
    }

    @Override
    public List<Role> getRole() {
        return roleDao.getRole();
    }

    @Override
    public List<RoleVo> getAllRole() {
        return roleDao.getAllRole();
    }

    @Override
    public Integer[] getMenuIds(Integer roleId) {
        return roleDao.getMenuIds(roleId);
    }

    @Override
    public int insertRole(Role role) {
        return roleDao.insertRole(role);
    }

    @Override
    public int insertRoleMenu(Integer roleId,Integer menuId) {
        return roleDao.insertRoleMenu(roleId,menuId);
    }

    @Override
    public int deleteRole(Integer roleId) {
        return roleDao.deleteRole(roleId);
    }

    @Override
    public int deleteRoleMenu(Integer roleId) {
        return roleDao.deleteRoleMenu(roleId);
    }

    @Override
    public int updateRole(Role role) {
        return roleDao.updateRole(role);
    }

    @Override
    public List<RoleVo> queryRoles(String str) {
        List<RoleVo> roleVoList = new ArrayList<>();
        List<Role> roleList = roleDao.queryRoles(str);
        for (Role role:roleList) {
            Integer[] menuIds =  roleDao.getMenuIds(role.getRoleId());   //通过角色id  查询其权限集合
            RoleVo roleVo = new RoleVo();
            roleVo.setRoleId(role.getRoleId());
            roleVo.setRoleName(role.getRoleName());
            roleVo.setRemark(role.getRemark());
            roleVo.setMenuIds(menuIds);

            roleVoList.add(roleVo);
        }
        return roleVoList;
    }
}
